<template>
    <div>
        <el-dialog :title="dialogInfo.isAdd ? '添加轮播图' : '编辑轮播图'" :visible.sync="dialogInfo.isShow"
            :before-close="cancel" center>
            <el-form :model="bannerInfo" :rules="rules" ref="bannerInfo">
                <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
                    <el-input v-model="bannerInfo.title" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图片" :label-width="formLabelWidth">
                    <el-upload action="#" list-type="picture-card" :auto-upload="false" :limit="1" :file-list="fileList"
                        :on-preview="onPreview" :on-remove="handleRemove" :on-change="onChange" :on-exceed="onExceed">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="" />
                    </el-dialog>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="bannerInfo.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { banneradd, bannerinfo, banneredit } from '../../request/api'
import { mapActions, mapGetters } from "vuex";
export default {
    props: ["dialogInfo"],
    data() {
        return {
            bannerInfo: {
                title: '',
                img: '',
                status: 1
            },
            formLabelWidth: "120px", //label宽度
            dialogVisible: false,  //用于控制图片预览的显示隐藏
            //预览时调用的图片地址
            dialogImageUrl: "",
            fileList: [], //用于图片回显
            rules: {
                title: [
                    // required必须的 必传项
                    { required: true, message: "请输入标题", trigger: "blur" },
                    { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
                ],
            },
        };
    },
    computed: {
        ...mapGetters({
            bannerList: 'banner/getBannerList'
        })
    },
    mounted() {
        this.getBanner()
    },
    methods: {
        ...mapActions({
            getBanner: 'banner/changeBannerAction'
        }),
        lookup(id) {
            bannerinfo({ id }).then((res) => {
                if (res.data.code == 200) {
                    this.bannerInfo = res.data.list
                    // 图片回显
                    this.fileList = this.bannerInfo.img ? [{ url: this.bannerInfo.img }] : [];
                    this.bannerInfo.id = id
                } else {
                    this.$message.error(res.data.msg);
                }
            })
        },
        // 限制图片文件
        onExceed(files, fileList) {
            this.$message.warning(
                `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length
                } 个文件`
            );
        },
        //预览的回调函数
        onPreview(file) {
            console.log(file, "文件");
            //得到文件地址之后，把它赋值给要显示的属性
            this.dialogImageUrl = file.url
            //打开图片预览弹框
            this.dialogVisible = true;
        },
        // 移除图片的时候触发
        handleRemove(file, fileList) {
            console.log(file, "移除", fileList);
        },
        // 当文件发生变化的时候触发
        onChange(file, fileList) {
            console.log(file, "文件详情，change时候");
            //把文件详情赋值给图片地址
            this.bannerInfo.img = file.raw
        },
        cancel() {
            this.$emit('cancel', false),
                this.reset()
        },
        reset() {
            this.bannerInfo = {
                title: '',
                img: '',
                status: 1
            }
            this.$refs.bannerInfo.resetFields();
            this.fileList = [];
        },
        confirm() {
            this.$refs.bannerInfo.validate((valid) => {
                if (valid) {
                    if (this.dialogInfo.isAdd) {
                        banneradd(this.bannerInfo).then((res) => {
                            if (res.data.code == 200) {
                                this.getBanner()
                                this.cancel()
                                this.$message.success(res.data.msg);
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        })
                    } else {
                        banneredit(this.bannerInfo).then((res) => {
                            if (res.data.code == 200) {
                                this.getBanner()
                                this.cancel()
                                this.$message.success(res.data.msg);
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        })
                    }
                } else {
                    this.$message.error("验证失败");
                    return false;
                }
            })
        }
    }
};
</script>

<style scoped>
</style>
